package com.chenglu.android.common.utils;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;

import static android.content.ContentValues.TAG;

/**
 *  @author lyj
 *  @Created by Copyright © 2023 Guangzhou Hongma Technology Co., Ltd. All rights reserved.
 *  @Description：sp工具类
 */
public class SPUtils {
    private static Application application = null;

    public static void setApplication(Application application) {
        if (application == null) {
            Log.d(TAG, "Setting Application was null, please check this.");
            return;
        }
        SPUtils.application = application;
    }

    public static void setApplication(Activity activity) {
        if (activity == null) {
            Log.d(TAG, "Setting activity was null, please check this.");
            return;
        }
        SPUtils.application = activity.getApplication();
    }


    private static Context context;
    private static SharedPreferences sPrefs;

    private SPUtils(Context context, String fileName) {
        this.context = context;
        sPrefs = context.getSharedPreferences(
                fileName, Context.MODE_PRIVATE);
    }

    public static void init(Context context, String fileName) {
        new SPUtils(context, fileName);
    }

    public static String fileName;

    public static int getInt(String key, int defaultValue) {
        return sPrefs.getInt(key, defaultValue);
    }

    public static void setInt(String key, int value) {
        sPrefs.edit().putInt(key, value).commit();
    }

    public static boolean getBoolean(String key, boolean defaultValue) {
        return sPrefs.getBoolean(key, defaultValue);
    }

    public static void setBoolean(String key, boolean value) {
        sPrefs.edit().putBoolean(key, value).commit();
    }

    public static String getString(String key, String defaultValue) {
        if (sPrefs == null)
            return defaultValue;
        return sPrefs.getString(key, defaultValue);
    }

    public static void setString(String key, String value) {
        if (sPrefs == null)
            return;
        sPrefs.edit().putString(key, value).commit();
    }

    public static void clearAll() {
        if (sPrefs == null)            return;
        sPrefs.edit().clear().commit();
    }

    public static void remove(String key) {
        if (sPrefs == null)
            return;
        sPrefs.edit().remove(key).commit();
    }


}